package com.evan.uyclub.Touxiang;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.text.format.Time;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.evan.uyclub.Other.LoadDialog;
import com.evan.uyclub.Other.Utlis;
import com.evan.uyclub.Other.VPNActivity;
import com.evan.uyclub.R;
import com.joooonho.SelectableRoundedImageView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.skydoves.elasticviews.ElasticCardView;
import com.squareup.picasso.Picasso;
import com.tapadoo.alerter.Alerter;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class ActivityTouxiang extends AppCompatActivity {
    String Tows1 = "http://api.btstu.cn/sjtx/api.php?lx=b1&format=json";
    String Tows2 = "http://api.btstu.cn/sjtx/api.php?lx=a1&format=json";
    String Tows3 = "http://api.btstu.cn/sjtx/api.php?lx=c2&format=json";
    String Tows4 = "http://api.btstu.cn/sjtx/api.php?lx=c3&format=json";
    int time = 300;
    private ImageView touImage, touImage1, touImage2, back, dow_tow;
    private SelectableRoundedImageView TypeImage1, TypeImage2, TypeImage3;
    private ElasticCardView Tow1, Tow2, Tow3, Tow4;
    private SmartRefreshLayout smart;
    private LoadDialog loadDialog;

    @Override
    protected void onCreate(Bundle b) {
        super.onCreate(b);
        //vpn检测
        VPNActivity vpnActivity = new VPNActivity(this);
        setContentView(R.layout.activity_touxiang);
        touImage = findViewById(R.id.touImage);
        touImage1 = findViewById(R.id.touImage1);
        touImage2 = findViewById(R.id.touImage2);
        TypeImage1 = findViewById(R.id.TypeImage1);
        TypeImage2 = findViewById(R.id.TypeImage2);
        TypeImage3 = findViewById(R.id.TypeImage3);
        Start(Tows1);

        //弹出预加载
        loadDialog = new LoadDialog(this);
        loadDialog.show(false);
        new Handler().postDelayed(this::Back, time);
        //调用函数实例，
        //  要提供Contect和数据
        //需要在import里引入调用的函数名
        Utlis.Puss(ActivityTouxiang.this);
        //返回调用函数
        back = findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        //回弹布局SmartreRreshLayout效果
        smart = findViewById(R.id.smart);
        smart.setEnablePureScrollMode(false);
        smart.setEnableLoadMore(false);

        //下拉更新；
        smart.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                Start(Tows1);
                refreshLayout.finishRefresh();
            }
        });

        //获取更新按钮进行更新头像数据
        Rel();

        dow_tow = findViewById(R.id.dow_tow);
        dow_tow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Time t = new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料。
                t.setToNow(); // 取得系统时间。
                String time = t.year + "年 " + (t.month + 1) + "月 " + t.monthDay + "日 " + t.hour + "h " + t.minute + "m " + t.second;
                Bitmap bitmap = ((BitmapDrawable) touImage.getDrawable()).getBitmap();  //获取图片
                String bb = Utlis.fileSaveToPublic(view.getContext(), "BakImg" + t.second, bitmap);
                // mMyDialog.dismiss();
                Alerter.create((Activity) view.getContext())
                        .setDuration(1200)
                        .setTitle("壁纸已保存至相册！")
                        .setText("保存路径：" + bb)
                        .setBackgroundColorRes(R.color.fbutton_color_emerald)
                        .show();
            }

        });
    }


    private void Back() {
        loadDialog.dismiss();
    }

    public void Start(String Tows1) {
        if (!Utlis.isVPNConnected(this)) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        URL url = new URL(Tows1);
                        URLConnection urlConnection = url.openConnection();
                        InputStreamReader inputStreamReader = new InputStreamReader(urlConnection.getInputStream());
                        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                        String data = bufferedReader.readLine();
                        //函数调用实例
                        //Json(ActivityTouxiang.this,data);
                        Datajson(data);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            }).start();
        }

    }

    public void Datajson(String data) throws JSONException {

        JSONObject jsonObject = new JSONObject(data);
        String jsondata = jsonObject.getString("imgurl");
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Picasso.with(ActivityTouxiang.this).load(jsondata).noPlaceholder().error(R.mipmap.yujiazai).into(touImage);
                Picasso.with(ActivityTouxiang.this).load(jsondata).noPlaceholder().error(R.mipmap.yujiazai).into(touImage1);
                Picasso.with(ActivityTouxiang.this).load(jsondata).noPlaceholder().error(R.mipmap.yujiazai).into(touImage2);
                Picasso.with(ActivityTouxiang.this).load(jsondata).noPlaceholder().error(R.mipmap.yujiazai).into(TypeImage1);
                Picasso.with(ActivityTouxiang.this).load(jsondata).noPlaceholder().error(R.mipmap.yujiazai).into(TypeImage2);
                Picasso.with(ActivityTouxiang.this).load(jsondata).noPlaceholder().error(R.mipmap.yujiazai).into(TypeImage3);
            }
        });
    }

    public void Rel() {
        Tow1 = findViewById(R.id.Tow1);
        Tow1.setOnClickListener(this::oncK);
        Tow2 = findViewById(R.id.Tow2);
        Tow2.setOnClickListener(this::oncK);
        Tow3 = findViewById(R.id.Tow3);
        Tow3.setOnClickListener(this::oncK);
        Tow4 = findViewById(R.id.Tow4);
        Tow4.setOnClickListener(this::oncK);
    }

    private Void oncK(View view) {
        switch (view.getId()) {
            case R.id.Tow1:
                Start(Tows1);
                loadDialog.show(false);
                new Handler().postDelayed(this::Back, time);
                break;
            case R.id.Tow2:
                Start(Tows2);
                loadDialog.show(false);
                new Handler().postDelayed(this::Back, time);
                break;
            case R.id.Tow3:
                Start(Tows3);
                loadDialog.show(false);
                new Handler().postDelayed(this::Back, time);
                break;
            case R.id.Tow4:
                Start(Tows4);
                loadDialog.show(false);
                new Handler().postDelayed(this::Back, time);
                break;
        }
        return null;
    }
}
